package Controller;

import Model.Application.Exception.CalculateApplicationException;
import View.IView;
import Model.Application.IModel;
import Share.Constante.ConstanteIdModeServer;
import Model.Application.Exception.ApplicationException;
import Share.Constante.ConstanteIdException;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

/**
 *
 * @author SpAdE
 */
public class Controller implements IController {

    IModel M;
    IView V;
    int mode;

    public Controller(IModel M, IView V) {
        this.M = M;
        this.V = V;

        this.V.setView();
        this.V.setMenu();

    }

    @Override
    public void init() {
        V.setActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().matches("year")) {
            this.mode = ConstanteIdModeServer.NOMBRE_ANNEE;
            V.setForm(mode);//showForm
        }
        if (ae.getActionCommand().matches("annualInstalment")) {
            this.mode = ConstanteIdModeServer.ANNUITE;
            V.setForm(mode);
        }
        if (ae.getActionCommand().matches("calculate")) {
            try {
                if (this.mode == ConstanteIdModeServer.NOMBRE_ANNEE) {
                    if ((V.getAmount() != -1) && V.getRate() != -1 && V.getAnnualInstalment() != -1) {
                        V.displayResult(this.mode, M.calculate(this.mode, V.getAmount(), V.getRate(), V.getAnnualInstalment()));
                    }else
                        V.showError(ConstanteIdException.INPUT_VALUE_FONCTIONAL_EXCEPTION);
                } else if (this.mode == ConstanteIdModeServer.ANNUITE) {
                    if ((V.getAmount() != -1) && V.getRate() != -1 && V.getYear() != -1) {
                        V.displayResult(this.mode, M.calculate(this.mode, V.getAmount(), V.getRate(), V.getYear()));
                    }else
                        V.showError(ConstanteIdException.INPUT_VALUE_FONCTIONAL_EXCEPTION);
                }
            } catch (ApplicationException aex) {
                V.showError(aex.getId());
                V.setMenu();
            }
        }
        if (ae.getActionCommand().matches("restart")) {
            V.setMenu();

        }
        if (ae.getActionCommand().matches("english")) {
            V.setLocal("en", "US");
            V.setMenu();
            System.out.println("us");

        }
        if (ae.getActionCommand().matches("french")) {
            V.setLocal("fr", "FR");
            V.setMenu();
            System.out.println("fr");

        }
    }

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        try {
            double close = M.calculate(ConstanteIdModeServer.BYE, 0, 0, 0.0);
            System.exit(0);
            if (close != -1) {
                throw new CalculateApplicationException(ConstanteIdException.CLOSE_STREAM_TRANSPORT_EXCEPTION);
            }
            System.out.println("closed");
        } catch (CalculateApplicationException caex) {
            V.showError(caex.getId());
            V.setMenu();
        }
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }
}
